My Stats

Row

Chart A: Network Graph

Row

Total People I’ve Interacted with

15

Days

12

Number of places I’ve been

3

Chart B: Places I’ve Been

NYC Stats

Row

Chart A: Stats by Zip

Row

Chart B: Cases over Time


Chart B: Cases by Borough

---
title: "My Personal Corona Tracker"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
  
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(magrittr)
library(igraph)
library(stringr)
library(RColorBrewer)
library(visNetwork)
library(usmap)
library(plotly)
library(curl)
library(ggmap)

###load in Data###

#for network graph
cal <- read.csv('~/Desktop/corona_tracker/data/calendar.csv')
edges <- cal %>%  reshape2::melt(id.vars  = c("source","target","vibe","county","weight"), value.name = "date")
edges %<>% filter(!is.na(date)) %>% filter(date != '')
edges$source %<>% str_trim()
edges$target %<>% str_trim()
edges$date %<>% lubridate::mdy()
edges %<>% filter (Sys.Date() - date <= 14)
edges %<>% select(-variable) %>% group_by(source,target) %>% 
  mutate(weight = ifelse(is.na(weight), n(),weight)) %>% 
  filter(date == max(date)) %>% ungroup() 

edges <- edges[!duplicated(edges),]


##make node graph from edges graph 
nodes <- data.frame(nodes = unique(edges$source), id = 1:length(unique(edges$source)))
nodes %<>% rbind(data.frame(nodes = unique(edges$target), id = 1:length(unique(edges$target))))
 nodes %<>% filter(!duplicated(nodes))

#for county graph
county_info <- edges$county %>% stringr::str_split(pattern = ", ") %>% unlist() %>% unique()
counties <- usmap::us_map(regions = "counties", include= c(.mid_atlantic, .new_england)) 
counties %<>% mutate(beenthere = ifelse(county %in% county_info, T,F))


##

#nyc static info
nyc_map <- get_map(location = c(lon = -74.00, lat = 40.71), maptype = "terrain", zoom = 11)

#zip to boro
zip_2_boro <- read.csv('~/Desktop/corona_tracker/data/zip_borough.csv')

#zip to lat long 
gc <- read.csv('~/Desktop/corona_tracker/data/latlong.csv')

#for daily cases
tests_day <- read.csv(curl("https://raw.githubusercontent.com/nychealth/coronavirus-data/master/trends/tests.csv") )
tests_day$DATE %<>% lubridate::mdy()

### last 7 days info
neighborhoodinfo <- read.csv(curl("https://raw.githubusercontent.com/nychealth/coronavirus-data/master/latest/last7days-by-modzcta.csv"))

#positive by zip since august 
zip_perc_positive <- read.csv(curl("https://raw.githubusercontent.com/nychealth/coronavirus-data/master/trends/percentpositive-by-modzcta.csv") )
zip_perc_positive %<>% select(week_ending,matches('PCTPOS_1'))
zip_perc_positive %<>% reshape2::melt(id.vars = "week_ending", variable.name = "zip",value.name = "percent_positive")
zip_perc_positive$zip %<>% str_remove("PCTPOS_")
 
#case rates by zip since august
zip_case_rates <- read.csv(curl("https://raw.githubusercontent.com/nychealth/coronavirus-data/master/trends/caserate-by-modzcta.csv") )
zip_case_rates %<>% select(week_ending,matches('CASERATE_1'))
zip_case_rates %<>% reshape2::melt(id.vars = "week_ending", variable.name = "zip",value.name = "case_rate")
zip_case_rates$zip %<>% str_remove("CASERATE_")

zip_info <- inner_join(zip_case_rates,zip_perc_positive)
zip_info$zip %<>% as.integer()

boro <-  zip_info %>% left_join(., zip_2_boro, by = "zip")

boro %<>% group_by(borough, week_ending) %>%
  select(-zip) %>%
  summarise_all(mean,na.rm= T) 

boro$week_ending %<>% as.Date(tryFormats = c("%m/%d/%Y"))
 
```

My Stats
=====================================  

Row {data-width=800 }
-----------------------------------------------------------------------
### Chart A: Network Graph

```{r}

g <- graph_from_data_frame(d=edges, vertices=nodes, directed=FALSE)

coul  <- brewer.pal(4, "Set2") 

t <- toVisNetworkData(g, idToLabel = TRUE)


t$edges$value <- log(t$edges$weight) * .6  + 4
t$edges$title <- paste(t$edges$weight, "day(s) last 2 weeks")
t$edges$color <- coul[as.numeric(as.factor(t$edges$vibe))]
t$edges %<>% mutate(length = ifelse((weight == 30 | weight == 15),10,250))
ledges <- data.frame(color = unique(coul[as.numeric(as.factor(t$edges$vibe))]),
                     label = unique(t$edges$vibe))
set.seed(13)
visNetwork(nodes = t$nodes, edges = t$edges, physics = T, main = "Contacts In the Last 2 weeks") %>% 
  visNodes(shape = 'circle',
           color = list(background = "white", 
                        border = "darkblue",
                        highlight = "yellow")) %>% 
  visLegend(addEdges = ledges,width = 0.2, position = "left", ncol = 3, stepY = 50)


```

Row {data-width=300}
--------------------------------------
### Total People I've Interacted with 
```{r}
npeople = edges %>% filter(source == 'simone' | target == 'simone') %>% summarise(n = n())
valueBox(value = npeople,color = "orange",caption = "People I've Interacted With")
```

-----------------------------------------------------------------------

### Days 
```{r}
days  = as.numeric(Sys.Date() - as.Date('2021-02-12'))
valueBox(value = days,color = "lightblue", caption = "Days since last Covid Test")
```

-----------------------------------------------------------------------

### Number of places I've been 
```{r}
countiez  = length(county_info)
valueBox(value = countiez,color = "lightpink", caption = "Counties I've Been To")
```


### Chart B: Places I've Been

```{r}

coul_2  <- brewer.pal(2, "Pastel2") 
# Create a vector of color
my_color_2 <- coul_2[as.numeric(as.factor(counties$beenthere))]

p <-plot_usmap(regions = 'counties', include= c(.mid_atlantic, .new_england), 
           fill = my_color_2, alpha = .7)+ 
  geom_point(data = counties, aes(x = x, y = y, text = county),alpha = 0) + 
  labs(title = 'Places Ive Been')

ggplotly(p, tooltip = c('text')) 
```


NYC Stats
=====================================  
Row {data-width=800 }
-----------------------------------------------------------------------
### Chart A: Stats by Zip

```{r}

formap <- cbind(neighborhoodinfo, gc)
formap %<>% rename(percent_positive = percentpositivity_7day)

t <- ggmap(nyc_map) + geom_point(
  aes(x=lon, y=lat, colour=percent_positive, size = percent_positive,
      text = paste(modzcta_name, "
", "Percent Positive:",percent_positive,"
", "Case Count:", people_positive,"
", "People Tested:",people_tested)), data=formap, na.rm = T,show.legend =F) + scale_color_gradient() + labs(title = paste0("Case Count by Zip: ", unique(formap$daterange)))+ theme(legend.position='none', axis.text.x = element_blank(), axis.text.y = element_blank(), axis.ticks = element_blank(), rect = element_blank(), axis.title.y=element_blank(),axis.title.x=element_blank()) t %>% ggplotly(tooltip = c('text')) ``` Row {data-width=400} --------------------------------------------------------------------- ### Chart B: Cases over Time ```{r} pos_day <- tests_day %>% dplyr::select(DATE, PERCENT_POSITIVE_7DAYS_AVG, POSITIVE_TESTS_7DAYS_AVG) %>% rename(date = DATE, positive_rate = PERCENT_POSITIVE_7DAYS_AVG, total_positive_tests = POSITIVE_TESTS_7DAYS_AVG) %>% mutate(positive_rate = positive_rate*100) %>% ggplot(aes(x = date,label=positive_rate)) + geom_line(aes(y = total_positive_tests, color = 'red'), size = 1.2, show.legend = F) + ylab("total positive tests") + theme_minimal()+ labs(title = "Citywide Daily Case Count") + theme(legend.position='none') pos_day %>% ggplotly(tooltip = c("date","label","y")) ``` --------------------------------------------------------------------- ### Chart B: Cases by Borough ```{r} boro_g <- boro %>% filter(!is.na(borough)) %>% ggplot(aes(x = week_ending,text = paste(week_ending, "
","Percent Positive:",round(percent_positive,2),"
", "Case Count per 100K:", round(case_rate,2)))) + geom_line(aes(y = round(percent_positive,2), group = borough, color = borough), size = 1.2) + theme(axis.text.x=element_text(angle=90,hjust=1)) + xlab('month') + ylab('percent positive') + labs(title = "Percent Positive by Borough") + theme_minimal() boro_g %>% ggplotly(tooltip = c('text')) ```